package com.ujoku.service.impl;

import com.labillusion.core.util.StringUtils;
import com.ujoku.service.CdnService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Greg on 14-10-25.
 */
@Service
public class CdnServiceImpl implements CdnService {

    @Value("${settings.cnd.domain}")
    private String domain;

    public String builder(String uri, long timestamp){
        if(!StringUtils.hasText(uri))
            return null;

        uri = domain + uri + "?lastmod=" + timestamp;
        return uri;
    }

    @Override
    public String buildGoodsImage(int id,String type, long timestamp){
        String item = domain + "goods/" + id + "_" + type + "0.jpg" + "?lastmod=" + timestamp;
        return item;
    }

    /**
     *
     * http://img.jiaoyi9.com/ujoku/goods/100_medium0.jpg
     * @param id
     * @param type
     * @return
     */
    @Override
    public List<String> buildGoodsImages(int id,String type, long timestamp){

        List<String> list = new ArrayList<>();
        for(int i = 0;i < 5;i++){
            String item = domain + "goods/" + id + "_" + type + i + ".jpg" + "?lastmod=" + timestamp;
            list.add(item);
        }

        return list;
    }

    @Override
    public List<String> buildGoodsDescImages(int id, long timestamp){
        List<String> list = new ArrayList<>();
        for(int i = 0;i < 4;i++){
            String item =  domain + "goods/" + id + "_desc"+i+".jpg" + "?lastmod=" + timestamp;
            list.add(item);
        }

        return list;
    }

    @Override
    public String buildBannerImage(int id, long timestamp){
        String item = domain + "activity/act" + id + ".jpg" + "?lastmod=" + timestamp;
        return item;
    }

    @Override
    public String portraitBuilder(String uri, long timestamp){
        return uri + "?lastmod=" + timestamp;
    }

    @Override
    public String buildTopBannerImage(int id, long timestamp){
        String item = domain + "topBanner/" + id + ".jpg" + "?lastmod=" + timestamp;
        return  item;
    }

}
